package com.maxatime.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.dao.ClassroomMapper;
import com.maxatime.dao.EmployeeInfoMapper;
import com.maxatime.dao.GradeClassroomRelationMapper;
import com.maxatime.dao.GradeMapper;
import com.maxatime.dao.StudentBasicInfoMapper;
import com.maxatime.pojo.dto.GradeDTO;
import com.maxatime.pojo.entity.Classroom;
import com.maxatime.pojo.entity.EmployeeInfo;
import com.maxatime.pojo.entity.Grade;
import com.maxatime.pojo.entity.GradeClassroomRelation;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.service.GradeService;
import com.maxatime.service.GradeStudentRelationService;
import com.maxatime.util.SerialNumberUtil;


@Service
public class gradeServiceImpl implements GradeService{
	
	Logger log = LogManager.getLogger();
	
	@Autowired
	GradeMapper gradeMapper;
	
	@Autowired
	EmployeeInfoMapper employeeInfoMapper;//讲师和班主任
	
	@Autowired
	ClassroomMapper classroomMapper;//教室
	
	@Autowired
	GradeClassroomRelationMapper gradeClassroomRelationMapper;//第二个表
	
	@Autowired 
	StudentBasicInfoMapper StudentBasicInfoMapper;//详情跳转查询学生信息
	
	//查询
	@Override
	public Map<String, Object> listData(GradeDTO gradeDTO) {
		// TODO Auto-generated method stub
		Grade grade1 = new Grade();
		BeanUtils.copyProperties(gradeDTO, grade1);
		
		grade1.setDeleteFlag("0");//查看删除状态为空或者其他的都不显示
		//分页
		PageHelper.startPage(grade1.getPageNum(),grade1.getPageSize());
		
		List<Grade> list = gradeMapper.listData(grade1);
		List<GradeDTO> listDTO = new ArrayList<GradeDTO>();
		
		PageInfo<Grade> pageInfo= new PageInfo<Grade>(list);
		Map<String, Object> m =new HashMap<>();
	    m.put("list", list);
	    m.put("pageInfo", pageInfo);
	    
		
		for (int  i=0;i<list.size();i++) {
			GradeDTO gradeDTO1 = new GradeDTO();
			BeanUtils.copyProperties(list.get(i), gradeDTO1);
			listDTO.add(gradeDTO1);
		}

		
		return m;
	}

	
	
	//删除
	@Override
	public int deleteByPrimaryKey(String gradeId) {
		// TODO Auto-generated method stub
		gradeMapper.deleteByPrimaryKey1(gradeId);
		gradeClassroomRelationMapper.deleteByPrimaryKey(gradeId);
		return 0;
	}
	//反显
	@Override
	public GradeDTO findgradeId(String gradeId) {
		// TODO Auto-generated method stub
		Grade grade = gradeMapper.findgradeId(gradeId);
		GradeDTO gradeDTO = new GradeDTO();
		BeanUtils.copyProperties(grade, gradeDTO);
		return gradeDTO;
	}

	//新增
	@Override
	@Transactional
	public int insertSelective(GradeDTO record) {
		// TODO Auto-generated method stub
		GradeClassroomRelation gradeClassroomRelation = new GradeClassroomRelation();//第二个表
		Grade grade=new Grade();//第一个表
		BeanUtils.copyProperties(record, grade);
		grade.setDeleteFlag("0");//默认值
		Date d=new Date();//默认值
		grade.setCreateTime(d);//默认值
		grade.setUpdateTime(d);//默认值
		String gradeId=String.valueOf(SerialNumberUtil.getRandomNum());
		grade.setGradeId(gradeId);//默认值
		String studentsNumber = String.valueOf(SerialNumberUtil.getRandomNum());
		grade.setStudentsNumber(Integer.parseInt(studentsNumber));
		
		gradeClassroomRelation.setGradeId(gradeId);
		gradeClassroomRelation.setDeleteFlag("0");
		gradeClassroomRelation.setCreateTime(d);
		gradeClassroomRelation.setUpdateTime(d);
		gradeClassroomRelation.setClassroomClassroomId("6");
		gradeMapper.insertSelective(grade);
		gradeClassroomRelationMapper.insertSelective(gradeClassroomRelation);
		
		return 0;
	}


	//新增班主任讲师
	@Override
	public List<EmployeeInfo> teacher(EmployeeInfo record) {
		// TODO Auto-generated method stub
		List<EmployeeInfo> list = employeeInfoMapper.teacher(record);
		return list;
	}


	//教室
	@Override
	public List<Classroom> classroom(Classroom record) {
		// TODO Auto-generated method stub
		List<Classroom> list=classroomMapper.classroom(record);
		return list;
	}


	//修改
	@Override
	public int updateByPrimaryKeySelective(GradeDTO record) {
		// TODO Auto-generated method stub
		
		Grade grade=new Grade();
		BeanUtils.copyProperties(record, grade);
		gradeMapper.updateByPrimaryKeySelective(grade);
		return 0;
	}


	//跳转详情页面
	@Override
	public Grade selectByPrimaryKey(String gradeId) {
		// TODO Auto-generated method stub
		Grade grade = gradeMapper.selectByPrimaryKey(gradeId);
		return grade;
	}



	@Override
	public List<GradeStudentRelationService> updateStu(String gradeId) {
		// TODO Auto-generated method stub
		gradeMapper.updateStu(gradeId);
		return null;
	}


	//查询柱状形态图
	@Override
	public List<Grade> listData2(Grade grade) {
		// TODO Auto-generated method stub
		
		return gradeMapper.listData2(grade);
	}



	
}
